package com.enfi.api.modules.db.service.impl;

import com.enfi.api.modules.db.base.BaseQuery;
import com.enfi.api.modules.db.base.BaseServiceImpl;
import com.enfi.api.modules.db.dao.WarningViewRepository;
import com.enfi.api.modules.db.entity.WarningView;
import com.enfi.api.modules.db.entity.Workorder;
import com.enfi.api.modules.db.entity.WorkorderUser;
import com.enfi.api.modules.db.service.WarningViewService;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.SpeUtils;
import com.enfi.api.util.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 预警內容浏览表(WarningView)表服务实现类
 *
 * @author wind
 * @since 2020-04-07 15:59:43
 */
@Service("warningViewService")
@NoRepositoryBean
public class WarningViewServiceImpl extends BaseServiceImpl<WarningViewRepository,WarningView,Integer> implements WarningViewService {
    @Override
    public List findByUserCodeIn(Collection userCodeList) {
        return this.baseRepository.findByUserCodeIn(userCodeList);
    }

    @Override
    protected Object query(BaseQuery baseQuery, WarningView warningView, Pageable pageable) {
        Assert.isTrue(baseQuery.getQuery()!=null&&baseQuery.getQuery().getQueryType()!=null,"查询方式不为空");
        switch (baseQuery.getQuery().getQueryType()){
            case "":
                SecUser secUser= UserUtils.getSecUser();
                Page<WarningView> page= this.baseRepository.findAll((root, query, builder)->{
                    List<Predicate> predicates= SpeUtils.getInstance().toSpec(warningView,baseQuery,root,builder);
                    query.where(predicates.stream().toArray(Predicate[]::new));
                    return null;
                },pageable);
                Set<String> userSet=page.getContent().stream().map(v->v.getUserId()).collect(Collectors.toSet());
                List list1= this.findByUserCodeIn(userSet);
                Map map= Maps.newHashMap();
                if(!CollectionUtils.isEmpty(list1)){
                    list1.forEach(v->{
                        Object[] arr=(Object[])v;
                        map.put(arr[0],arr);
                    });
                }
                page.getContent().forEach(v->{
                    Object[] arr=(Object[]) map.get(v.getUserId());
                    if(!Objects.isNull(arr)){
                        v.setPost(arr[2].toString());
                        v.setPostName(arr[3].toString());
                    }
                });
                return page;
            default:
                return super.query(baseQuery,warningView,pageable);
        }
    }
}